package com.syni.mdd.yhd.common.all.utils;

import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.DateTimeUtils;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.service.BmsCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @className IdentifyCode
 * @description 6位数验证码生成
 * @Author cfx
 * @DATE 2019/5/5 15:43
 * @VERSION 1.0
 **/
@Component
public class IdentifyCode {

    @Value("${app.identify.code.send.num:200}")
    static int identifyCodeSendNum;

    static final String redisKey = Constants.REDIS_KEY_IDENTCODE;

    static int identifyCodeExpire;

    static RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        IdentifyCode.redisTemplate = redisTemplate;
    }

    @Value("${app.identify.code.expire}")
    public void setIdentifyCodeExpire(String identifyCodeExpire) {
        IdentifyCode.identifyCodeExpire = Integer.parseInt(identifyCodeExpire);
    }

    @Value("${app.identify.code.send.num:200}")
    public void setIdentifyCodeSendNum(String identifyCodeSendNum) {
        IdentifyCode.identifyCodeSendNum = Integer.parseInt(identifyCodeSendNum);
    }

    public static boolean checkPhoneIdentifySendNum(String phone){
        Double score = redisTemplate.opsForZSet().score(redisKey + "send:num",phone);
        if(score != null) {
            if (score.intValue() > identifyCodeSendNum) {
                return true;
            }
        }
        return false;
    }

    public static String getIdentifyCode(){
        StringBuilder stringBuilder = new StringBuilder();
        for(int ii = 0;ii < 6;ii++){
            int code = (int)(Math.random() * 10);
            stringBuilder.append(code);
        }
        return stringBuilder.toString();
    }

    public static void addIdentifyCodeToRedis(String userPhone,String code,Integer codeType){
        redisTemplate.opsForValue().set(redisKey  + codeType + ":" + userPhone,code,identifyCodeExpire, TimeUnit.MINUTES);
        if(redisTemplate.hasKey(redisKey + "send:num")){
            redisTemplate.opsForZSet().incrementScore(redisKey + "send:num" ,userPhone,Integer.parseInt(Constants.COMMON_FLAG_TRUE));
        }else {
            redisTemplate.opsForZSet().incrementScore(redisKey + "send:num" ,userPhone,Integer.parseInt(Constants.COMMON_FLAG_TRUE));
            //设置过期时间
            long restSecond = DateTimeUtils.getEndMill();
            redisTemplate.expire(redisKey + "send:num",restSecond,TimeUnit.MILLISECONDS);
        }
    }

    public static boolean checkIdentifyCode(String userPhone, String code,Integer codeType){
        if(ApplicationContextHolder.checkRunProfile(ApplicationContextHolder.RUN_DEV,ApplicationContextHolder.RUN_LOCAL)){
            if(code.equals(Constants.SUPER_ID_CODE)){
                return true;
            }
        }
        if(code.equals(Constants.SKIP_CAPTCHA)){
            return true;
        }
        String key = redisKey + codeType + ":" + userPhone;
        if(redisTemplate.hasKey(key)){
            String identifyCode = redisTemplate.opsForValue().get(key).toString();
            if(identifyCode != null && identifyCode.equals(code)){
                return true;
            }
        }
        if(BmsCommonService.checkRunProfile(BmsCommonService.RUN_PRO)){
            return false;
        }
        return true;
    }

    public static boolean checkIdentifyCode(String userPhone, String code,Integer... codeTypes){
        for(Integer codeType : codeTypes) {
            if(checkIdentifyCode(userPhone, code,codeType)){
                return true;
            }
        }
        return false;
    }

    public static Map<Integer,Integer> initBusinessClient(){
        final int BUS_REGISTER_CODY = 101;
        final int BUS_LOGIN_CODY = 102;
        final int BUS_ADD_AS_CODY = 103;
        //电享生活商户APP
        final int DX_BUS_REGISTER_CODY = 201;
        final int DX_BUS_FORGET_CODY = 202;
        final int DX_BUS_LOGIN_CODY = 203;
        final int DX_BUS_MODIFY_CODY = 204;
        Map<Integer,Integer> businessClient = new HashMap<>();
        businessClient.put(BUS_REGISTER_CODY,1);
        businessClient.put(BUS_LOGIN_CODY,1);
        businessClient.put(BUS_ADD_AS_CODY,1);

        businessClient.put(DX_BUS_REGISTER_CODY,1);
        businessClient.put(DX_BUS_FORGET_CODY,1);
        businessClient.put(DX_BUS_LOGIN_CODY,1);
        businessClient.put(DX_BUS_MODIFY_CODY,1);
        return businessClient;
    }
}
